package com.whacos.common.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.whacos.common.enums.Constant;
import com.whacos.common.enums.DelFlagEnum;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author xiongdun
 * @description 公共DTO
 * @since 2018/11/19 22:53
 */
@Data
public class BaseEntity implements Serializable {

    private static final long serialVersionUID = -2772148974991091611L;

    // 主键ID
    @TableId(value = "id", type = IdType.AUTO)
    protected Long id;
    // 创建者
    @TableField(value = "created_by")
    protected Long createdBy;
    // 创建时间
    @TableField(value = "created_date")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime createdDate;
    // 更新者
    @TableField(value = "updated_by")
    protected Long updatedBy;
    // 更新时间
    @TableField(value = "updated_date")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime updatedDate;
    // 备注信息
    @TableField(value = "remark")
    protected String remark;
    // 删除标记
    @TableField(value = "del_flag")
    protected Integer delFlag;

    public BaseEntity(Long id) {
        this.id = id;
    }

    public BaseEntity() {
        super();
    }

    public void createdData() {
        this.createdData(Constant.DEFAULT_USER_ID);
    }

    public void createdData(Long userId) {
        this.createdBy = userId;
        this.createdDate = LocalDateTime.now();
        this.updatedBy = userId;
        this.updatedDate = LocalDateTime.now();
        this.delFlag = DelFlagEnum.NORMAL.getValue();
        this.remark = "";
    }

    public void updatedData() {
        this.updatedBy = Constant.DEFAULT_USER_ID;
        this.updatedDate = LocalDateTime.now();
    }

    public void updatedData(Long userId) {
        this.updatedBy = userId;
        this.updatedDate = LocalDateTime.now();
    }

    public void deleteData() {
        this.deleteData(Constant.DEFAULT_USER_ID);
    }

    public void deleteData(Long userId) {
        this.updatedBy = userId;
        this.updatedDate = LocalDateTime.now();
        this.delFlag = DelFlagEnum.DELETE.getValue();
    }
}
